define(["jquery", "ec", "core/util"], function($, echarts, util) {
	var _icon=null,
		_PluginObject=null;
	var View = {

		config: function(title, xData, yData) {
			var option = {
				title: {
					text: title
				},
				tooltip: {
					trigger: 'axis'
				},
				legend: {
					data: ['会话数']
				},
				grid: {
					left: '3%',
					right: '4%',
					bottom: '3%',
					containLabel: true
				},
				toolbox: {
					feature: {
						saveAsImage: {}
					}
				},
				xAxis: {
					type: 'category',
					boundaryGap: false,
					data: xData
				},
				yAxis: {
					type: 'value'
				},
				series: [{
					name: '会话数',
					type: 'line',
					stack: '总量',
					data: yData
				}]
			};
			return option;
		},
		
		barConfig:function(title, xData, yData){
			
			return {
			    title: {
			        text: title
			    },
			    tooltip: {
			        trigger: 'item'
			    },
			    toolbox: {
			        show: true,
			        feature: {
			            dataView: {show: true, readOnly: false},
			            restore: {show: true},
			            saveAsImage: {show: true}
			        }
			    },
			    calculable: true,
			    grid: {
			        borderWidth: 0
			    },
			    xAxis: [
			        {
			            type: 'category',
			            show: true,
			            data: xData
			        }
			    ],
			    yAxis: [
			        {
			            type: 'value',
			            show: true
			        }
			    ],
			    series: [
			        {
			            name: '坐席会话数',
			            type: 'bar',
			            itemStyle: {
			                normal: {
			                    color: function(params) {
			                        var colorList = [
			                          '#C1232B','#B5C334','#FCCE10','#E87C25','#27727B',
			                           '#FE8463','#9BCA63','#FAD860','#F3A43B','#60C0DD',
			                           '#D7504B','#C6E579','#F4E001','#F0805A','#26C0C0'
			                        ];
			                        return colorList[params.dataIndex]
			                    },
			                    label: {
			                        show: true,
			                        position: 'top',
			                        formatter: '{c}'
			                    }
			                }
			            },
			            data: yData
			            
			        }
			    ]
			}
		},

		initView: function(_this) {
			var todaySessionCount = echarts.init(document.getElementById('report-t-session'));
			var agentSessionCount = echarts.init(document.getElementById('report-a-session'));
			var bsnssSessionCount = echarts.init(document.getElementById('report-b-session'));


			getData("h", function(x, y) {
				var o = View.config("今日小时会话数报表", x, y);
				todaySessionCount.setOption(o);
			})
			getData("a", function(x, y) {
				var o = View.barConfig("今日坐席会话数报表", x, y);
				agentSessionCount.setOption(o);
			})
			getData("b", function(x, y) {
				var o = View.barConfig("今日业务会话数报表", x, y);
				bsnssSessionCount.setOption(o);
			})

		}

	}

	function getData(type, callback) {
		var url = "";
		if (type == "h") {
			url = "/session/report/hour/session";
		} else if (type == "a") {
			url = "/session/report/agent/session";
		}else if(type=="b"){
			url = "/session/report/business/session";
		}
		util.get(url, function(data) {
			var x = [],
				y = [];
			if (data && data.length > 0) {
				data.forEach(function(val, index, _array) {
					x.push(val.LABEL);
					y.push(val.COUNT);
				})
			}
			callback(x, y);

		})

	}

	function openWin() {
		var $div = $("<div/>").appendTo($("body"));
		$div.window({
			width: 950,
			height: 450,
			title: "报表查看",
			href: "plugs/report/template.html",
			minimizable: false,
			maximizable: false,
			collapsible: false,
			shadow:false,
			onLoad: function() {
				var _this = $(this);
				View.initView(_this);
			},
			onClose: function() {
				$div.window("destroy", true);
			}
		})
	}

	return {

		init: function(icon,config) {
			_icon=icon;
			_PluginObject=config;
		},

		run: function() {
			openWin();
		}

	}

})